const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mask: false,     // 遮罩层
    title: "",       // 直播标题
    password: "",    // 密码
    liveId: ""       // 直播间id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 标题
  inputTitle(e){
    this.setData({
      title: e.detail.value
    })
  },

  // 密码
  inputPass(e){
    this.setData({
      password: e.detail.value
    })
  },
  // 申请直播
  applyLive() {
    if ( !app.validate( this.data.title, 'required')) {
      app.showModal('请输入直播标题');
      return;
    }

    const that = this;
    const param = {
      doctorId: wx.getStorageSync('userid'),
      liveName: this.data.title
    };
    app.request({
      url: "train/onlineVideo/generateLive",
      data: param,
      success: function (res) {
        console.log(res)
        that.setData({
          mask: true,
          liveId: res.data.liveId
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },
  // 确认开启直播
  sure(){
    this.setData({
      mask: false
    }) 
  },
  // 开启
  unlock() {

    if (!app.validate(this.data.password, 'required')) {
      app.showModal('请输入密码');
      return;
    }
    const that = this;
    const param = {
      liveId: this.data.liveId,
      livePassword: this.data.password
    };
    app.request({
      url: "train/onlineVideo/confirmLive",
      data: param,
      success: function (res) {
        const url = res.data.pushUrl;
        wx.navigateTo({
          url: '/pages/doctor-live/doctor-live?url=' + url
        });
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
   
  },

  // 联系客服
  handleContact(e) {
    console.log('客服消息')
    console.log(e);
  }

})